package com.toonly.dbgenerator;

import toonly.dbmanager.sqlbuilder.Insert;
import toonly.dbmanager.sqlbuilder.SQL;
import toonly.dbmanager.sqlbuilder.TableId;
import toonly.wrapper.SW;
import toonly.wrapper.StringWrapper;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;

/**
 * Created by caols on 10/4/15.
 */
public class MainFromCSV {

    public static void main(String[] args) throws FileNotFoundException {
        BufferedReader reader = new BufferedReader(new FileReader("经纬度.csv"));

        Insert insert = new Insert(new TableId("ycarapi", "data_service_area"), "name", "priority", "geo_locations");

        SW<Integer> lineCount = new SW<>(0);
        reader.lines().forEach((line) -> {
            Integer lineNum = lineCount.val();
            lineCount.val(lineNum + 1);
            if (lineNum == 0) {
                return;
            }

            String[] cells = line.split(",", 3);
            System.out.println(Arrays.toString(cells));

            String points = cells[2].replaceAll("\"", "").replaceAll("\\s", "").replaceAll(",*$", "").replaceAll(";", ",");

            checkPoints(points);

            insert.add(Arrays.asList(
                    new StringWrapper(cells[0]).wrap("\"", "\"").val(),
                    cells[1],
                    new StringWrapper(points).wrap("\"", "\"").val()
                    ));
        });

        SQL sql = insert::toSql;
        System.out.println(sql.toSql());
    }

    private static void checkPoints(String points) {
        String[] split = points.split(",");
        for (String s : split) {
            Double.valueOf(s);
        }
    }

}
